Skip to content

Make diff section contents red(-)/green(+) in run-tests.php #5965

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 2 commits into from

Conversation

TysonAndre
Copy link
Contributor

@TysonAndre TysonAndre commented Aug 9, 2020

This deliberately uses one color. Green would be associated with things that were done correctly.
But both additional lines and missing lines contribute to the failure,
so that may be confusing for something that's an error report instead of a diff
that is meant to be applied.

This uses green for lines with + and red for lines with -.

Red Colors(Red and Green) would make the failure causes stand out visually when scrolling through errors.

@Girgias @nikic - thoughts?

For example, https://docs.pytest.org/en/stable/getting-started.html seems to make the entire diff section red.
Some other tools like PHPUnit don't seem to colorize diffs at all, just the overall summary.

This deliberately uses only one color.
Green would be associated with things that were done correctly.
But both additional lines and missing lines contribute to the failure,
so that may be confusing for something that's an error report instead of a diff
that is meant to be applied.

Red would make the failure causes stand out visually when scrolling through
errors.
Copy link
Member

@Girgias Girgias left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me. :) would make it easier to find it on a CI output.

run-tests.php Outdated
@@ -2885,6 +2886,23 @@ function generate_array_diff(array $ar1, array $ar2, bool $is_reg, array $w): ar
$old1 = array();
$old2 = array();

$format_diff_line = function (int $line_number, string $contents, string $diff_character) use ($colorize): string {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't you move the global $colorize; inside this function body? As it's currently not used anywhere else by generate_array_diff()

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can, I didn't know how much reviewers would care about efficiency. I'd expect repeatedly looking up the same global on every closure call to make this a bit slower for diffs with a large amount of unexpected actual output, but the closure invocation itself would be a bit slower and this probably doesn't need to be optimized

The closure invocation itself would be much slower than the addition of adding a global $colorize;, though

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

tbh I didn't think about performance that much, but I could see it having an impact in that case where having it important once be better.

run-tests.php Outdated
Comment on lines 2899 to 2904
$format_expected_line = function (int $line_number, string $contents) use ($format_diff_line): string {
return $format_diff_line($line_number, $contents, '-');
};
$format_actual_line = function (int $line_number, string $contents) use ($format_diff_line): string {
return $format_diff_line($line_number, $contents, '+');
};
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would prefer that the expected and actual line be different colours, I don't know which ones to choose however. As I personally tend to get confused by which is which >.<

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Light red and dark red? (may be unreadable on screens/terminals with poor contrast with a black background)

Colorizing just the '-' and '+' differently is one other option, but I don't know how readable that would be

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Orange and Light red might be better? But, the more I think about it, the less I know what is a good solution to this problem, so maybe just having the whole diff as one colour is fine.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we want to color diffs, let's color them in the usual way (red and green)...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This deliberately uses one color. Green would be associated with things that were done correctly.
But both additional lines and missing lines contribute to the failure,
so that may be confusing for something that's an error report instead of a diff
that is meant to be applied.

That's what I initially planned to do, but I updated that before committing because it seemed misleading to use green for a line that's an error. Green definitely makes sense for git/github diffs, patch proposals, etc., but seems less clear for an error message

For example, if context was added (-C NUM_LINES, like diff -C 3), then light green would be appropriate for the surrounding lines that actually matched the expected lines (although light grey or no colorizing would be my preference for matching context lines)

Copy link
Member

@nikic nikic Aug 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you're overthinking this. The diff colors are not a value judgement. Red and green does not mean "bad" and "good", it just means "removed" and "added".

Adding some context to diffs sounds like a good idea.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding some context to diffs sounds like a good idea.

Agreed, I was wondering why that was never supported. I guess the line number of the expected(green) file could be used.

If there are substantial objections to the color, I guess an environment variable, CLI flag, or an RFC with secondary votes on the run-tests.php colorizing style could be created.

I was probably overthinking this out of concern of others objecting - I locally use colordiff for error reporting in another project without issues

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A screenshot of what it will look like

run-tests-output

@TysonAndre TysonAndre changed the title Make diff section contents red in run-tests.php Make diff section contents red(-)/green(+) in run-tests.php Aug 9, 2020
@TysonAndre
Copy link
Contributor Author

The build failure seems unrelated to this PR

@php-pulls php-pulls closed this in 58489bd Aug 10, 2020
TysonAndre added a commit to TysonAndre/php-src that referenced this pull request Aug 10, 2020
Mentioned in php#5965 (comment)

This PR proposes 3 lines of context so the impact can be seen in tests.
Other `diff` programs show around 3 lines of context.
(This helps indicate exactly which position a test should be updated
to add a new expected line at)
@nikic
Copy link
Member

nikic commented Aug 10, 2020

I have temporarily reverted this change, as it adds the coloring not just to the --show-diff output on the shell, but also to the .diff files, where it is not meaningful and makes things hard to read.

TysonAndre added a commit to TysonAndre/php-src that referenced this pull request Aug 12, 2020
Mentioned in php#5965 (comment)

This PR proposes 3 lines of context so the impact can be seen in tests.
Other `diff` programs show around 3 lines of context.
(This helps indicate exactly which position a test should be updated
to add a new expected line at)

Use the mapping for choosing order to display diffs

Properly include context in cases where the expected output had more lines than
the actual output, e.g.

```
--FILE--
A
A1
A
C
NEARBY
--EXPECTF--
A
B
A1
B
A
B
A
B
NEARBY
```
TysonAndre added a commit to TysonAndre/php-src that referenced this pull request Aug 15, 2020
Mentioned in php#5965 (comment)

This PR proposes 3 lines of context so the impact can be seen in tests.
Other `diff` programs show around 3 lines of context.
(This helps indicate exactly which position a test should be updated
to add a new expected line at)

Use the mapping for choosing order to display diffs

Properly include context in cases where the expected output had more lines than
the actual output, e.g.

```
--FILE--
A
A1
A
C
NEARBY
--EXPECTF--
A
B
A1
B
A
B
A
B
NEARBY
```
php-pulls pushed a commit that referenced this pull request Aug 16, 2020
Mentioned in #5965 (comment)

This PR proposes 3 lines of context so the impact can be seen in tests.
Other `diff` programs show around 3 lines of context.
(This helps indicate exactly which position a test should be updated
to add a new expected line at)

Use the mapping for choosing order to display diffs

Properly include context in cases where the expected output had more lines than
the actual output, e.g.

```
--FILE--
A
A1
A
C
NEARBY
--EXPECTF--
A
B
A1
B
A
B
A
B
NEARBY
```

Closes GH-5968
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants